<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModOnline {
	
	public function getIntervalData($params){
		$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';
		 
		$args = array(
				':t_s'    => $params['time_start'],
				':t_e'    => $params['time_end'],
				':g'      => empty($params['g']) ? 'ALL' : $params['g'],
				':period' => 'Daily'
		);
		 
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		 
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
	
		$table = 'tblgameonlineinterval';
	
		$sql = "SELECT * FROM {$table} {$where}";
	
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		$res = $dbLink->query($sql, $args);
		return $res;
	}

	/**
	 * 方法废弃使用
	 * @param unknown $params
	 * @return Ambigous <multitype:, unknown>
	 */
    public static function indexData($params){
        $table = 'tblOnlineTime';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end
                        AND Platform=:platform AND GameSrc=:game ';
        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':platform' => $params['os'],
            ':game'     => $params['g'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL' && $params['server'] !== 'LIST'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else{
            $where .= 'AND SID = "ALL"';
        }


        $sql = "SELECT * FROM {$table} {$where} order by RecDate desc, SID asc";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

} 